//给定一个二叉树的根节点 root ，返回它的 中序 遍历。 
//
// 
//
// 示例 1： 
//
// 
//输入：root = [1,null,2,3]
//输出：[1,3,2]
// 
//
// 示例 2： 
//
// 
//输入：root = []
//输出：[]
// 
//
// 示例 3： 
//
// 
//输入：root = [1]
//输出：[1]
// 
//
// 示例 4： 
//
// 
//输入：root = [1,2]
//输出：[2,1]
// 
//
// 示例 5： 
//
// 
//输入：root = [1,null,2]
//输出：[1,2]
// 
//
// 
//
// 提示： 
//
// 
// 树中节点数目在范围 [0, 100] 内 
// -100 <= Node.val <= 100 
// 
//
// 
//
// 进阶: 递归算法很简单，你可以通过迭代算法完成吗？ 
// Related Topics 栈 树 深度优先搜索 二叉树 👍 1184 👎 0

package leetcode.editor.cn;

import java.util.*;

public class _94_BinaryTreeInorderTraversal {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    static final int NULL = Integer.MAX_VALUE;

    public static void main(String[] args) {
//        System.out.println(NULL);
//        int[] nums = new int[] {1, NULL, 2, 3};
        int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7};

        TreeNode root = prepareTreeNode(nums, 0);
        Solution solution = new _94_BinaryTreeInorderTraversal().new Solution();
        List<Integer> res = solution.inorderTraversal(root);
        System.out.println(res);
    }

    private static TreeNode prepareTreeNode(int[] nums, int index) {
        if (index >= nums.length) {
            return null;
        }

        TreeNode root = new TreeNode(nums[index]);
        root.left = prepareTreeNode(nums, 2 * index + 1);
        root.right = prepareTreeNode(nums, 2 * index + 2);

        return root;
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {

        /**
         * Morris 中序遍历
         * @param root
         * @return
         */
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            TreeNode pre = null;

            while (root != null) {
                if (root.left != null) {
                    pre = root.left;
                    while (pre.right != null && pre.right != root) {
                        pre = pre.right;
                    }
                    if (pre.right == null) {
                        pre.right = root;
                        root = root.left;
                    } else {
                        res.add(root.val);
                        pre.right = null;
                        root = root.right;
                    }

                } else {
                    res.add(root.val);
                    root = root.right;
                }
            }
            return res;
        }

        /**
         * 这个是官方解法的栈
         * @param root
         * @return
         */
        public List<Integer> inorderTraversal_stack(TreeNode root) {
            Stack<TreeNode> st = new Stack<TreeNode>();
            List<Integer> res = new ArrayList<Integer>();

            while (root != null || !st.isEmpty()) {
                while (root != null) {
                    st.push(root);
                    root = root.left;
                }
                root = st.pop();
                res.add(root.val);
                root = root.right;
            }
            return res;
        }

        /**
         * 自己写的递归，还可以
         *
         * @param root
         * @return
         */
        public List<Integer> inorderTraversal_digui(TreeNode root) {
            List<Integer> res = new ArrayList<Integer>();
            zhong(root, res);
            return res;
        }

        private void zhong(TreeNode root, List<Integer> res) {
            if (root == null) {
                return;
            }

            zhong(root.left, res);
            res.add(root.val);
            zhong(root.right, res);
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}